package com.chinasoft.mall.activity.adapter;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.subActivity.goods.GoodsDetailActivity;
import com.chinasoft.mall.model.response.SearchGoodsBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetImageView;

public class SearchListAdapter extends BaseAdapter {
	private List<SearchGoodsBean> dds;
	private LayoutInflater inflater;
	private MainActivity MainActivity;
	private ViewHolder _vh;

	public SearchListAdapter(MainActivity MainActivity, List<SearchGoodsBean> crs) {
		inflater = LayoutInflater.from(MainActivity);
		this.MainActivity=MainActivity;
		this.dds = crs;
	}
	
	public void setData(List<SearchGoodsBean> crs){
		this.dds=crs;
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		if (dds == null) {
			return 0;
		} else {
			return dds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (dds == null) {
			return null;
		} else {
			return dds.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.sy_recomlv, null);
			_vh = new ViewHolder();
			_vh.modelimg=(NetImageView) convertView.findViewById(R.id.modelimg);
			_vh.spid=(TextView) convertView.findViewById(R.id.spid);
			_vh.sptitle=(TextView) convertView.findViewById(R.id.sptitle);
			_vh.newprice=(TextView) convertView.findViewById(R.id.newprice);
			_vh.oldprice=(TextView) convertView.findViewById(R.id.oldprice);
			convertView.setTag(_vh);
		} else {
			_vh = (ViewHolder) convertView.getTag();
		}
		SearchGoodsBean _dd=dds.get(position);
		_vh.modelimg.setDownloadurl(StringUtil.goodUrl(_dd.getGOODS_ID(),_dd.getTHUM()));
		_vh.spid.setText("19"+_dd.getGOODS_ID());
		_vh.sptitle.setText(_dd.getGOODS_NAME());
		_vh.newprice.setText("￥"+_dd.getPRC());
		_vh.oldprice.setText("￥"+_dd.getMPRC());
		convertView.setOnClickListener(new onClickImpl(MainActivity, dds, position));
		return convertView;
	}
	
	/**
	 * 跳转到通道详情
	 * 
	 * @author Administrator
	 * 
	 */
	class onClickImpl implements View.OnClickListener {
		private List<SearchGoodsBean> crs;
		private int position;
		private MainActivity MainActivity;

		public onClickImpl(MainActivity MainActivity, List<SearchGoodsBean> crs, int position) {
			this.crs = crs;
			this.position = position;
			this.MainActivity = MainActivity;
		}

		@Override
		public void onClick(View v) {
			SearchGoodsBean scd = crs.get(position);
			if (scd != null) {
				MainActivity.setCurrentActivity(GoodsDetailActivity.class, scd);
			}
		}
	}

	class ViewHolder {
		public NetImageView modelimg;
		public TextView spid;
		public TextView sptitle;
		public TextView newprice;
		public TextView oldprice;
	}
}
